﻿Imports System.Data

Partial Class Sistema_InternadosVisitaIndicadores
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Request.QueryString("idinternadoPersona") = Nothing And String.IsNullOrEmpty(hdidInternadoPersona.Value) Then
            Response.Redirect("Internados.aspx")
        End If
        If Request.QueryString("idInternadoIndicador") = Nothing And String.IsNullOrEmpty(hdidInternadoIndicador.Value) Then
            Response.Redirect("Internados.aspx")
        End If

        If Page.IsPostBack = False Then
            hdidInternadoIndicador.Value = Request.QueryString("idInternadoIndicador")
            hdidInternadoPersona.Value = Request.QueryString("idInternadoPersona")
            cargarDatos()
            cargarCriterios()
            CargarCasos()
        End If
    End Sub

    Private Sub cargarDatos()
        Using bd As New BdHogaresDataContext
            'Dim d = (From c In bd.Web_InternadosVisitas
            '        Where c.idinternadovisita = hdIdInternadoVisita.Value
            '        Select c.idInternado, c.TipoVisita, c.FechaDiligenciamiento, c.NombreProfesional, c.idinternadovisita,
            '        TipoVisitaV = (bd.fn_GetNombreOpcion("TipoVisita", c.TipoVisita))).FirstOrDefault

            'hdIdInternado.Value = d.idInternado
            'lblVisita.Text = d.TipoVisitaV & " Realizada el " & String.Format("{0:d}", d.FechaDiligenciamiento)

            'obtener el internado




            Dim b = (From c In bd.Web_internadosPersonas
                       Where c.idInternadoPersona = hdidInternadoPersona.Value
                       Select c).FirstOrDefault
            With b
                txtCicloEducativo.Text = .CicloEducativo
                txtCicloVital.Text = .CicloVital
                TxtConsecutivo.Text = .consecutivopersona
                txtNoParticipantes.Text = .NoParticipantes
                txtObservaciones.Text = .observacionesPersona
                hdIdInternado.Value = .idinternado
            End With



            Dim a = (From c In bd.Web_internados
                   Where c.idinternado = hdIdInternado.Value
                   Select c.Institucion, c.Municipio.Municipio, c.Municipio.Departamento.Departamento,
                   c.NombreProfesional, fechaDiligenciamiento = String.Format("{0:d}", c.FechaDiligenciamiento), localizacion = (bd.fn_GetNombreOpcion("Ubicacion", c.Localizacion)),
                   c.CodProyecto, c.Vereda, c.Corregimiento, c.Barrio, c.Comuna, c.Direccion
                   ).FirstOrDefault

            'txtFechaDiligenciamiento.Text = a.fechaDiligenciamiento
            lblDepartamento.Text = a.Departamento
            lblMunicipio.Text = a.Municipio
            lblCodProyecto.Text = a.CodProyecto
            txtInstitucion.Text = a.Institucion




            Dim e = (From c In bd.Web_internadosIndicadors
                    Where c.idInternadoIndicador = hdidInternadoIndicador.Value
                    Select c.Descripcion, c.Tag).FirstOrDefault

            lblindicador.Text = e.Descripcion


        End Using
    End Sub
    Sub cargarCriterios()
        Using bd As New BdHogaresDataContext
            Dim f = bd.getInternadosPersonaVisitas(hdidInternadoIndicador.Value, hdidInternadoPersona.Value)
            grdCriterios.DataSource = f
            grdCriterios.DataBind()
        End Using
    End Sub
    Sub CargarCasos()
        Using bd As New BdHogaresDataContext
            Dim a = From c In bd.Web_InternadosPersonaVisitaCasos
                    Where c.idinternadoIndicador = hdidInternadoIndicador.Value And c.idInternadoPersona = hdidInternadoPersona.Value
                    Select c.idInternadoPersonaVisitaCaso, c.CasoDetectado
            grdCasos.DataSource = a
            grdCasos.DataBind()
        End Using
    End Sub
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnCancerlar.Click
        Response.Redirect("InternadosPersonasDetalle.aspx?idinternado=" & hdIdInternado.Value & "&idInternadoPersona=" & hdidInternadoPersona.Value)
    End Sub

    Protected Sub btnGuardar_Click(sender As Object, e As System.EventArgs) Handles btnGuardar.Click
        Using bd As New BdHogaresDataContext
            For Each fila As GridViewRow In grdCriterios.Rows
                If fila.RowType = DataControlRowType.DataRow Then
                    If CType(fila.FindControl("hdGuardar"), HiddenField).Value = "1" Then
                        Dim idInternadoPersonaVisita As Double
                        If String.IsNullOrEmpty(CType(fila.FindControl("hdhdidInternadoPersonaVisita"), HiddenField).Value) Then
                            idInternadoPersonaVisita = 0
                        Else
                            idInternadoPersonaVisita = CDbl(CType(fila.FindControl("hdhdidInternadoPersonaVisita"), HiddenField).Value)
                        End If
                        Dim a = (From c In bd.Web_InternadosPersonaVisitas
                                Where c.idInternadoPersonaVisita = idInternadoPersonaVisita
                                Select c).FirstOrDefault
                        'Dim a As New Web_InternadosPersonaVisita()
                        Dim nuevo = 0
                        If a Is Nothing Then
                            a = New Web_InternadosPersonaVisita()
                            nuevo = 1
                        End If
                        With a
                            .idinternadoCriterio = CType(fila.FindControl("hdidinternadoCriterio"), HiddenField).Value
                            .idInternadoPersona = hdidInternadoPersona.Value
                            .idinternadovisita = CType(fila.FindControl("hdIdInternadoVisita"), HiddenField).Value

                            Using h As New Helper
                                .idUsuarioRegistro = h.GetUsuario(Context.User.Identity.Name).IdUsurio
                            End Using
                            .Observaciones = CType(fila.FindControl("txtObservaciones"), TextBox).Text
                            .RespuestaSino = CType(fila.FindControl("rbRespuestaSino"), RadioButtonList).SelectedValue
                            .Fecharegistro = Date.Now
                        End With
                        If nuevo = 1 Then
                            bd.Web_InternadosPersonaVisitas.InsertOnSubmit(a)
                        End If
                        bd.SubmitChanges()
                        bd.registrarLog(Context.User.Identity.Name, 86, Request.CurrentExecutionFilePath)
                    End If
                End If
            Next
        End Using
        cargarCriterios()
        lblMensaje.Text = "Se ha guardado la información"
    End Sub
    Dim previousCat As String = ""
    Dim FirstRow As Int16 = -1
    Protected Sub grdCriterios_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCriterios.RowDataBound


        If e.Row.RowType = DataControlRowType.DataRow Then
            'Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
            'hace RowSpan de los criterios
            '' '' ''If previousCat = e.Row.DataItem.Criterio Then
            '' '' ''    If grdCriterios.Rows(FirstRow).Cells(0).RowSpan = 0 Then
            '' '' ''        grdCriterios.Rows(FirstRow).Cells(0).RowSpan = 2
            '' '' ''    Else
            '' '' ''        grdCriterios.Rows(FirstRow).Cells(0).RowSpan += 1

            '' '' ''    End If
            '' '' ''    e.Row.Cells.RemoveAt(0)
            '' '' ''Else
            '' '' ''    e.Row.VerticalAlign = VerticalAlign.Top
            '' '' ''    'Maintain the category in memory      
            '' '' ''    previousCat = e.Row.DataItem.Criterio
            '' '' ''    FirstRow = e.Row.RowIndex
            '' '' ''End If

            'pone los si y los no
            Dim rb As RadioButtonList = CType(e.Row.FindControl("rbRespuestaSino"), RadioButtonList)
            rb.SelectedValue = e.Row.DataItem.RespuestaSino
        End If
    End Sub

    Protected Sub btnGaurdarCaso_Click(sender As Object, e As System.EventArgs) Handles btnGaurdarCaso.Click
        Dim idinternadovisitaInt As Integer
        Using bd As New BdHogaresDataContext
            Dim z = (From c In bd.Web_InternadosVisitas
                    Where c.idInternado = hdIdInternado.Value
                    Select c.idinternadovisita, c.TipoVisita
                    Order By TipoVisita).FirstOrDefault

            idinternadovisitaInt = z.idinternadovisita
        End Using
        Dim a As New Web_InternadosPersonaVisitaCaso()
        With a
            .CasoDetectado = txtCasoDetectado.Text
            .Fecharegistro = Date.Now
            .idinternadoIndicador = hdidInternadoIndicador.Value
            .idInternadoPersona = hdidInternadoPersona.Value
            .idinternadovisita = idinternadovisitaInt
            Using h As New Helper
                .idUsuarioRegistro = h.GetUsuario(Context.User.Identity.Name).IdUsurio
            End Using
        End With
        Using bd As New BdHogaresDataContext
            bd.Web_InternadosPersonaVisitaCasos.InsertOnSubmit(a)
            bd.SubmitChanges()
            bd.registrarLog(Context.User.Identity.Name, 86, Request.CurrentExecutionFilePath)
        End Using
        txtCasoDetectado.Text = ""
        CargarCasos()
    End Sub

    Protected Sub grdCasos_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCasos.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            CType(e.Row.FindControl("lblNumero"), Label).Text = e.Row.RowIndex + 1
        End If
    End Sub

    Protected Sub grdCasos_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCasos.RowCommand
        If e.CommandName = "Eliminar" Then
            Using bd As New BdHogaresDataContext
                Dim a = (From c In bd.Web_InternadosPersonaVisitaCasos
                        Where c.idInternadoPersonaVisitaCaso = CInt(e.CommandArgument)
                        Select c).First

                bd.Web_InternadosPersonaVisitaCasos.DeleteOnSubmit(a)
                bd.SubmitChanges()
                bd.registrarLog(Context.User.Identity.Name, 86, Request.CurrentExecutionFilePath)
            End Using
            CargarCasos()
        End If
    End Sub
End Class
